.TH std::decay 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::decay \- std::decay

.SH Synopsis
   Defined in header <type_traits>
   template< class T >              \fI(since C++11)\fP
   struct decay;

   Performs the type conversions equivalent to the ones performed when passing function
   arguments by value. Formally:

     * If T is "array of U" or reference to it, the member typedef type is U*.
     * Otherwise, if T is a function type F or reference to one, the member typedef
       type is std::add_pointer<F>::type.
     * Otherwise, the member typedef type is
       std::remove_cv<std::remove_reference<T>::type>::type.

   If the program adds specializations for std::decay, the behavior is undefined.

.SH Member types

   Name Definition
   type the result of applying the decay type conversions to T

.SH Helper types

   template< class T >                       \fI(since C++14)\fP
   using decay_t = typename decay<T>::type;

.SH Possible implementation

   template<class T>
   struct decay
   {
   private:
       typedef typename std::remove_reference<T>::type U;
   public:
       typedef typename std::conditional<
           std::is_array<U>::value,
           typename std::add_pointer<typename std::remove_extent<U>::type>::type,
           typename std::conditional<
               std::is_function<U>::value,
               typename std::add_pointer<U>::type,
               typename std::remove_cv<U>::type
           >::type
       >::type type;
   };

.SH Example


// Run this code

 #include <type_traits>

 template<typename T, typename U>
 constexpr bool is_decay_equ = std::is_same_v<std::decay_t<T>, U>;

 int main()
 {
     static_assert
     (
         is_decay_equ<int, int> &&
         ! is_decay_equ<int, float> &&
         is_decay_equ<int&, int> &&
         is_decay_equ<int&&, int> &&
         is_decay_equ<const int&, int> &&
         is_decay_equ<int[2], int*> &&
         ! is_decay_equ<int[4][2], int*> &&
         ! is_decay_equ<int[4][2], int**> &&
         is_decay_equ<int[4][2], int(*)[2]> &&
         is_decay_equ<int(int), int(*)(int)>
     );
 }

.SH See also

   remove_cvref        combines std::remove_cv and std::remove_reference
   (C++20)             \fI(class template)\fP
   implicit conversion array-to-pointer, function-to-pointer, lvalue-to-rvalue
                       conversions
